package com.example.remotecontrol.ui.remote

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.ArrowDropUp
import androidx.compose.material.icons.filled.AspectRatio
import androidx.compose.material.icons.filled.Contrast
import androidx.compose.material.icons.filled.Fullscreen
import androidx.compose.material.icons.filled.Hd
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Input
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.OfflineBolt
import androidx.compose.material.icons.filled.Power
import androidx.compose.material.icons.filled.Remove
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Timer
import androidx.compose.material.icons.filled.WbAuto
import androidx.compose.material.icons.filled.WbIncandescent
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel

/**
 * 投影仪遥控器
 */
@Composable
fun ProjectorRemoteScreen(
    viewModel: RemoteViewModel = hiltViewModel()
) {
    val selectedDevice by viewModel.selectedDevice.collectAsState()
    val irAvailable by viewModel.irAvailable.collectAsState()
    
    // 投影仪状态
    var isPoweredOn by remember { mutableStateOf(false) }
    var selectedSource by remember { mutableStateOf(ProjectorSource.HDMI1) }
    
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 设备名称
            Text(
                text = selectedDevice?.name ?: "投影仪遥控器",
                style = MaterialTheme.typography.titleLarge,
                textAlign = TextAlign.Center
            )
            
            if (!irAvailable) {
                Spacer(modifier = Modifier.height(8.dp))
                Text(
                    text = "您的设备不支持红外功能",
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.error,
                    textAlign = TextAlign.Center
                )
            }
            
            Spacer(modifier = Modifier.height(24.dp))
            
            // 电源按钮
            ProjectorButton(
                icon = Icons.Default.Power,
                text = if (isPoweredOn) "关闭" else "打开",
                color = if (isPoweredOn) Color(0xFF4CAF50) else MaterialTheme.colorScheme.primary,
                size = 64.dp,
                onClick = {
                    isPoweredOn = !isPoweredOn
                    viewModel.sendIRCommand("POWER")
                },
                enabled = irAvailable
            )
            
            Spacer(modifier = Modifier.height(32.dp))
            
            // 如果电源关闭，不显示其他控制
            if (isPoweredOn) {
                // 输入源
                Card(
                    modifier = Modifier.fillMaxWidth(),
                    elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
                    shape = RoundedCornerShape(16.dp)
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "输入源",
                            style = MaterialTheme.typography.titleMedium
                        )
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 输入源按钮行1
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            ProjectorSourceButton(
                                label = "HDMI 1",
                                isSelected = selectedSource == ProjectorSource.HDMI1,
                                onClick = {
                                    selectedSource = ProjectorSource.HDMI1
                                    viewModel.sendIRCommand("HDMI1")
                                },
                                enabled = irAvailable
                            )
                            
                            ProjectorSourceButton(
                                label = "HDMI 2",
                                isSelected = selectedSource == ProjectorSource.HDMI2,
                                onClick = {
                                    selectedSource = ProjectorSource.HDMI2
                                    viewModel.sendIRCommand("HDMI2")
                                },
                                enabled = irAvailable
                            )
                            
                            ProjectorSourceButton(
                                label = "USB",
                                isSelected = selectedSource == ProjectorSource.USB,
                                onClick = {
                                    selectedSource = ProjectorSource.USB
                                    viewModel.sendIRCommand("USB")
                                },
                                enabled = irAvailable
                            )
                        }
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 输入源按钮行2
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            ProjectorSourceButton(
                                label = "VGA",
                                isSelected = selectedSource == ProjectorSource.VGA,
                                onClick = {
                                    selectedSource = ProjectorSource.VGA
                                    viewModel.sendIRCommand("VGA")
                                },
                                enabled = irAvailable
                            )
                            
                            ProjectorSourceButton(
                                label = "视频",
                                isSelected = selectedSource == ProjectorSource.VIDEO,
                                onClick = {
                                    selectedSource = ProjectorSource.VIDEO
                                    viewModel.sendIRCommand("VIDEO")
                                },
                                enabled = irAvailable
                            )
                            
                            ProjectorSourceButton(
                                label = "无线",
                                isSelected = selectedSource == ProjectorSource.WIRELESS,
                                onClick = {
                                    selectedSource = ProjectorSource.WIRELESS
                                    viewModel.sendIRCommand("WIRELESS")
                                },
                                enabled = irAvailable
                            )
                        }
                    }
                }
                
                Spacer(modifier = Modifier.height(24.dp))
                
                // 梯形校正和焦距
                Card(
                    modifier = Modifier.fillMaxWidth(),
                    elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
                    shape = RoundedCornerShape(16.dp)
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "画面调整",
                            style = MaterialTheme.typography.titleMedium
                        )
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 梯形校正控制
                        Text(
                            text = "梯形校正",
                            style = MaterialTheme.typography.bodyMedium
                        )
                        
                        Spacer(modifier = Modifier.height(8.dp))
                        
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.Center
                        ) {
                            // 垂直梯形校正+/-
                            Column {
                                ProjectorButton(
                                    icon = Icons.Default.KeyboardArrowUp,
                                    onClick = { viewModel.sendIRCommand("KEYSTONE_UP") },
                                    enabled = irAvailable
                                )
                                
                                Spacer(modifier = Modifier.height(8.dp))
                                
                                ProjectorButton(
                                    icon = Icons.Default.KeyboardArrowDown,
                                    onClick = { viewModel.sendIRCommand("KEYSTONE_DOWN") },
                                    enabled = irAvailable
                                )
                            }
                            
                            Spacer(modifier = Modifier.width(24.dp))
                            
                            // 水平梯形校正+/-
                            Column {
                                ProjectorButton(
                                    icon = Icons.Default.KeyboardArrowLeft,
                                    onClick = { viewModel.sendIRCommand("KEYSTONE_LEFT") },
                                    enabled = irAvailable
                                )
                                
                                Spacer(modifier = Modifier.height(8.dp))
                                
                                ProjectorButton(
                                    icon = Icons.Default.KeyboardArrowRight,
                                    onClick = { viewModel.sendIRCommand("KEYSTONE_RIGHT") },
                                    enabled = irAvailable
                                )
                            }
                        }
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        Divider()
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 焦距调整
                        Text(
                            text = "焦距",
                            style = MaterialTheme.typography.bodyMedium
                        )
                        
                        Spacer(modifier = Modifier.height(8.dp))
                        
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.Center
                        ) {
                            ProjectorButton(
                                icon = Icons.Default.Remove,
                                text = "焦距-",
                                onClick = { viewModel.sendIRCommand("FOCUS_MINUS") },
                                enabled = irAvailable
                            )
                            
                            Spacer(modifier = Modifier.width(24.dp))
                            
                            ProjectorButton(
                                icon = Icons.Default.WbAuto,
                                text = "自动对焦",
                                onClick = { viewModel.sendIRCommand("AUTO_FOCUS") },
                                enabled = irAvailable
                            )
                            
                            Spacer(modifier = Modifier.width(24.dp))
                            
                            ProjectorButton(
                                icon = Icons.Default.Add,
                                text = "焦距+",
                                onClick = { viewModel.sendIRCommand("FOCUS_PLUS") },
                                enabled = irAvailable
                            )
                        }
                    }
                }
                
                Spacer(modifier = Modifier.height(24.dp))
                
                // 导航和菜单控制
                Card(
                    modifier = Modifier.fillMaxWidth(),
                    elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
                    shape = RoundedCornerShape(16.dp)
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "菜单导航",
                            style = MaterialTheme.typography.titleMedium
                        )
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 方向键
                        Box(
                            modifier = Modifier.fillMaxWidth(),
                            contentAlignment = Alignment.Center
                        ) {
                            Column(
                                horizontalAlignment = Alignment.CenterHorizontally
                            ) {
                                // 上
                                ProjectorButton(
                                    icon = Icons.Default.KeyboardArrowUp,
                                    onClick = { viewModel.sendIRCommand("UP") },
                                    enabled = irAvailable
                                )
                                
                                Spacer(modifier = Modifier.height(8.dp))
                                
                                // 左/确定/右
                                Row(
                                    verticalAlignment = Alignment.CenterVertically
                                ) {
                                    // 左
                                    ProjectorButton(
                                        icon = Icons.Default.KeyboardArrowLeft,
                                        onClick = { viewModel.sendIRCommand("LEFT") },
                                        enabled = irAvailable
                                    )
                                    
                                    Spacer(modifier = Modifier.width(8.dp))
                                    
                                    // 确认
                                    ProjectorButton(
                                        text = "OK",
                                        size = 56.dp,
                                        onClick = { viewModel.sendIRCommand("OK") },
                                        enabled = irAvailable
                                    )
                                    
                                    Spacer(modifier = Modifier.width(8.dp))
                                    
                                    // 右
                                    ProjectorButton(
                                        icon = Icons.Default.KeyboardArrowRight,
                                        onClick = { viewModel.sendIRCommand("RIGHT") },
                                        enabled = irAvailable
                                    )
                                }
                                
                                Spacer(modifier = Modifier.height(8.dp))
                                
                                // 下
                                ProjectorButton(
                                    icon = Icons.Default.KeyboardArrowDown,
                                    onClick = { viewModel.sendIRCommand("DOWN") },
                                    enabled = irAvailable
                                )
                            }
                        }
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 菜单按钮
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            ProjectorButton(
                                icon = Icons.Default.Menu,
                                text = "菜单",
                                onClick = { viewModel.sendIRCommand("MENU") },
                                enabled = irAvailable
                            )
                            
                            ProjectorButton(
                                icon = Icons.Default.Home,
                                text = "主页",
                                onClick = { viewModel.sendIRCommand("HOME") },
                                enabled = irAvailable
                            )
                            
                            ProjectorButton(
                                icon = Icons.Default.Settings,
                                text = "设置",
                                onClick = { viewModel.sendIRCommand("SETTINGS") },
                                enabled = irAvailable
                            )
                        }
                    }
                }
                
                Spacer(modifier = Modifier.height(24.dp))
                
                // 其他功能
                Card(
                    modifier = Modifier.fillMaxWidth(),
                    elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
                    shape = RoundedCornerShape(16.dp)
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "其他功能",
                            style = MaterialTheme.typography.titleMedium
                        )
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 其他功能按钮行1
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            ProjectorButton(
                                icon = Icons.Default.WbIncandescent,
                                text = "灯光模式",
                                onClick = { viewModel.sendIRCommand("LAMP_MODE") },
                                enabled = irAvailable
                            )
                            
                            ProjectorButton(
                                icon = Icons.Default.OfflineBolt,
                                text = "节能",
                                onClick = { viewModel.sendIRCommand("ECO_MODE") },
                                enabled = irAvailable
                            )
                            
                            ProjectorButton(
                                icon = Icons.Default.Timer,
                                text = "定时",
                                onClick = { viewModel.sendIRCommand("SLEEP_TIMER") },
                                enabled = irAvailable
                            )
                        }
                        
                        Spacer(modifier = Modifier.height(16.dp))
                        
                        // 其他功能按钮行2
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            ProjectorButton(
                                icon = Icons.Default.AspectRatio,
                                text = "宽高比",
                                onClick = { viewModel.sendIRCommand("ASPECT_RATIO") },
                                enabled = irAvailable
                            )
                            
                            ProjectorButton(
                                icon = Icons.Default.Contrast,
                                text = "图像模式",
                                onClick = { viewModel.sendIRCommand("PICTURE_MODE") },
                                enabled = irAvailable
                            )
                            
                            ProjectorButton(
                                icon = Icons.Default.Fullscreen,
                                text = "全屏",
                                onClick = { viewModel.sendIRCommand("BLANK") },
                                enabled = irAvailable
                            )
                        }
                    }
                }
            }
            
            Spacer(modifier = Modifier.height(24.dp))
        }
    }
}

/**
 * 投影仪按钮
 */
@Composable
fun ProjectorButton(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    icon: ImageVector? = null,
    text: String? = null,
    size: Double = 48.0,
    color: Color = MaterialTheme.colorScheme.primary,
    enabled: Boolean = true
) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(
            modifier = modifier
                .size(size.dp)
                .clip(CircleShape)
                .background(if (enabled) MaterialTheme.colorScheme.surface else MaterialTheme.colorScheme.surfaceVariant)
                .border(
                    width = 1.dp,
                    color = if (enabled) color else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f),
                    shape = CircleShape
                )
                .clickable(enabled = enabled, onClick = onClick),
            contentAlignment = Alignment.Center
        ) {
            if (icon != null) {
                Icon(
                    imageVector = icon,
                    contentDescription = text,
                    tint = if (enabled) color else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
                )
            } else if (text != null) {
                Text(
                    text = text,
                    style = MaterialTheme.typography.bodyMedium,
                    color = if (enabled) color else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
                )
            }
        }
        
        if (text != null && icon != null) {
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = text,
                style = MaterialTheme.typography.bodySmall,
                color = if (enabled) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
            )
        }
    }
}

/**
 * 投影仪输入源按钮
 */
@Composable
fun ProjectorSourceButton(
    label: String,
    isSelected: Boolean,
    onClick: () -> Unit,
    enabled: Boolean = true
) {
    Box(
        modifier = Modifier
            .clip(RoundedCornerShape(4.dp))
            .background(
                if (isSelected) MaterialTheme.colorScheme.primaryContainer
                else MaterialTheme.colorScheme.surfaceVariant
            )
            .clickable(enabled = enabled, onClick = onClick)
            .padding(horizontal = 16.dp, vertical = 8.dp),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = label,
            style = MaterialTheme.typography.bodyMedium,
            color = if (isSelected) MaterialTheme.colorScheme.primary
                   else if (enabled) MaterialTheme.colorScheme.onSurfaceVariant
                   else MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.38f)
        )
    }
}

/**
 * 投影仪输入源
 */
enum class ProjectorSource {
    HDMI1, HDMI2, USB, VGA, VIDEO, WIRELESS
} 